﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Device.Location;
using System.Globalization;
using System.IO.IsolatedStorage;
using System.Linq;
using FastestWays.Model;
using Microsoft.Phone.Maps.Services;

namespace FastestWays.Server
{
    public class AddressesEngine
    {

        public AddressesEngine()
        {
            AddressesInSearch = new ObservableCollection<Address>();
            LoadAddressListFromIsoStorage();
        }

        #region IsoStorageAddress

        public List<Address> LoadAddressListFromIsoStorage()
        {
            var historyAddresses = new List<Address>();
            if (IsolatedStorageSettings.ApplicationSettings.Any())
            {
                foreach (var setting in IsolatedStorageSettings.ApplicationSettings)
                {
                    if (setting.Value is Address)
                    {
                        historyAddresses.Add((Address)setting.Value);                        
                    }
                }
            }
            return historyAddresses;
        }

        public void SaveAddressListToIsoStorage(List<Address> historyAddresses)
        {
            int i = 0;
            IsolatedStorageSettings.ApplicationSettings.Clear();

            foreach (var address in historyAddresses)
            {
                ++i;
                IsolatedStorageSettings.ApplicationSettings.Add(i.ToString(CultureInfo.InvariantCulture), address);
            }
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        #endregion

        #region QuerySearchAddress

        private
        ObservableCollection<Address> _addressesInSearch;
        public ObservableCollection<Address> AddressesInSearch
        {
            get { return _addressesInSearch; }
            set { _addressesInSearch = value; }
        }

        public void GetAddressesList(string addressText)
        {
            if (!string.IsNullOrEmpty(addressText))
            {
                GeocodeQuery destinationQuery = new GeocodeQuery()
                {
                    GeoCoordinate = new GeoCoordinate(32.1624, 34.8544),
                    SearchTerm = addressText
                };

                destinationQuery.QueryCompleted += destinationQuery_QueryCompleted;
                destinationQuery.QueryAsync();
            }
        }


        void destinationQuery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
        {
            //AddressesInSearch.surpressEvents = 
            AddressesInSearch.Clear();
            if (e.Result.Any())
            {
                foreach (var mapLocation in e.Result)
                {
                    AddressesInSearch.Add(new Address()
                        {
                            AddressPhrase = GetAddressString(mapLocation.Information.Address), 
                            Geocoordinate = mapLocation.GeoCoordinate, 
                            CurrentCountry = mapLocation.Information.Address.CountryCode
                        });
                }
            }
            else
            {
                AddressesInSearch.Add(new FakeAddress());
            }
        }

        private string GetAddressString(MapAddress address)
        {
            return string.Format("{0} {1} {2}", address.Street, address.HouseNumber, address.City);
        }
        #endregion
    }
}
